package com.javaweb.filter;

import com.javaweb.common.DBUtil;
import com.javaweb.common.DispatcherTool;
import com.javaweb.common.NullableTool;
import com.javaweb.common.StringTool;
import com.javaweb.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "FilterFreeLogin"
         /*  urlPatterns = {"/loginServlet"}*/

)
public class FilterFreeLogin implements Filter {
    public static final String FREE_COOKIE = "free" ;
    @Override
    public void destroy() {
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {


        // get the freelogin cookie
        HttpServletRequest request = (HttpServletRequest) req ;
        System.out.println("[INFO] entered freeLogin filter for request : " + request.getRequestURI()) ;
        if (! StringTool.isEmpty(request.getParameter("name"),
                         request.getParameter("pwd"))) {
            System.out.println("[INFO] pass to the back: for name " + request.getParameter("name")) ;
            chain.doFilter(req, resp);
            return ;
        }
        System.out.println("[INFO] filterFreelogin : get the cookies from request.") ;
        Cookie[] cookies = request.getCookies() ;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(FREE_COOKIE)) {
                String[] tokens = StringTool.decry(cookie.getValue());
                System.out.println("[INFO] freeLoginFilter got decoded: " + tokens[0] + " , "
                        + tokens[1]);
                request.getRequestDispatcher("loginServlet?name="
                         + tokens[0] + "&pwd=" + tokens[1]).forward(req, resp);
                return ;
            }
//
//                if (tokens != null) {
//                    User user = DBUtil.userLogin(tokens[0], tokens[1]) ;
//                    if (user != null ) {
//                        HttpSession session = ((HttpServletRequest) req).getSession() ;
//                        session.setAttribute("user", user);
//                        DispatcherTool.indirectForwardToClient(request, (HttpServletResponse)resp, "01/servlet/welcome.jsp", "登录成功！");
//                        return ;
//                    } else {
//                        // return the login.jsp
//                        System.out.println("failed in validating user.") ;
//                        DispatcherTool.indirectForwardToClient(request, (HttpServletResponse)resp, "01/servlet/login.jsp", "免登录失败！");
//                        return ;
//                    }
//
//                } else {
//                    // return the login.jsp
//                    System.out.println("failed in decoding cookie.") ;
//                    DispatcherTool.indirectForwardToClient(request, (HttpServletResponse)resp, "01/servlet/login.jsp", "免登录失败！");
//                    return ;
//                }
//            }
        }
        // for normal login request OR without freelogin cookie.
        chain.doFilter(req, resp);

    }
    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
